<?php
//问卷id
$wid = intval($_REQUEST['wid']);

$wenjuan = $Db->query("select * from ".$Base->table('wenjuan')." where id=$wid","Row");

if(!$wenjuan){
	die('no data exists');
}

$wenjuan['jiangxiang'] = $Json->decode($wenjuan['jiangxiang']);

$act = Common::sfilter($_REQUEST['act']);

$re['status'] = 0;
//显示抽奖界面
if($act=='jiang'){

	$step = Common::sfilter($_REQUEST['step']);
	//提交手机号
	if($step=='phone'){
		$phone = $_REQUEST['phone'];

		if(Common::is_mobile($phone)){

			$user = $Db->query("select user from ".$Base->table('wenjuan_fankui')." where user ='".$phone."' AND reward != '' AND wid= '".$wid."'",'Row');
			if($user){
				$re['msg'] = '您已经抽过奖了！';
			}else{
				$_SESSION['phone'] = $phone;
				$re = array('status'=>1,'msg'=>'现在开始答题','href'=>'/wenjuan?wid='.$wid);
					
			}
			echo $Json->encode($re);
			exit;
			
		}else{
			$re['msg'] = "请输入正确格式的手机号码！";
			echo $Json->encode($re);
			exit;
		}
	}
	//执行抽奖
	else if($step=='choujiang'){
		//未提交手机号，跳转到抽奖页
		if(empty($_SESSION['phone'])){
			Common::base_header("Location:".$_lang['host']."wenjuan?wid=$wid&act=jiang");
		}

		$reward = Common::sfilter($_REQUEST['reward']);

		if($reward == 'reward'){
			$phone = $_SESSION['phone'];

			$sta = $Db->query("select * from ".$Base->table('wenjuan_fankui')." where user ='$phone' AND wid= '".$wid."' order by id DESC","Row");

			if($sta['status'] > 0){
				$re['msg'] = "您已经抽过奖了！";
			}else if(strtotime($wenjuan['expire_time']) < Common::gmtime()){
				$re['msg'] = '您来晚了，活动已经过期了！';
			}
			else{

				$data['status'] = 1;

				$all_reward = $wenjuan['jiangxiang'];
				
				$pro = $wenjuan['pro'];

				$pro_num = rand(1,100);

				if($pro_num <= $pro){
					//随机一个礼品
					$keeping = true;
					do{
						//检查是否有剩余
						$all_remain = 0;
						foreach($all_reward as $v){
							$all_remain += $v['remain'];
						}
						if($all_remain==0){
							$keeping = false;
						}
						else{
							//随机提取一个
							$re_tmp = array_rand($all_reward);
							if($all_reward[$re_tmp]['remain'] > 0){
								$name = $all_reward[$re_tmp]['name'];
								$all_reward[$re_tmp]['remain'] =$all_reward[$re_tmp]['remain'] - 1;
								$keeping = false;
							}
						}	
					}
					while($keeping);
					$data['reward'] = $name;
					$data['code'] = Common::get_rand_number();
				}
				
				if(empty($name)){
					//unset($_SESSION['phone']);
					$re['msg'] = "您来晚了，奖品已经被抽完了！";
				}else{
					// $data['code'] = Common::get_rand_number();
					// $data['reward'] = $name;
					$new_jiangxiang['jiangxiang'] = $Json->encode_unescaped_unicode($all_reward);
					$Db->beginTransaction();
					$Db->update($Base->table('wenjuan'),$new_jiangxiang,array('id'=>$wid));
					$Db->update($Base->table('wenjuan_fankui'),$data,array('id'=>$sta['id']));
					$Db->commit();
					$re = array('status'=>1,'msg'=>'你抽中：'.$name,'code'=>$data['code']);
				}
			}
			echo $Json->encode($re);
			exit;
		}

		//显示这确率是否达到抽奖标准
		$num = $Db->query("select right_num from ".$Base->table('wenjuan_fankui')." where user ='".$_SESSION['phone']."' order by id desc",'Row');

		$num_wen = $Db->query("select score from ".$Base->table('wenjuan')." where id =".$wid,'Row');

		if($num['right_num'] >= $num_wen['score']){
			$massege = "1";
		}else{
			$massege = "0";
		}
		
		$tp->assign('massege',$massege);
	}
	//显示界面
	else{
		//todo
	}
}
//答题
else{
	if($_REQUEST['refresh']==1){
		unset($_SESSION['phone']);
	}
	//未提交手机号，跳转到抽奖页
	if(empty($_SESSION['phone'])){
		Common::base_header("Location:".$_lang['host']."wenjuan?wid=$wid&act=jiang");
	}
	$sql = "select * from ".$Base->table('subject')." where wid=$wid ORDER BY sort";
	$subjects = $Db->query($sql);
	//题目
	if($subjects){
		foreach($subjects as $k=>$v){
			$num = 0;
			$subjects[$k]['values']=$Json->decode($v['values']);
			foreach ($subjects[$k]['values'] as $key => $value) {
				if($value['selected'] >0){
					$num++;
				}
			}
			$subjects[$k]['num'] = $num;
		}
	}

	if(!empty($_POST)){
		$num = 0;
		$subject_sub = $_POST;
		unset($subject_sub['wid']);
		//print_r($subjects);
		//print_r($subject_sub);
		if(count($subject_sub) != count($subjects)){
			foreach($subjects as $k=>$v){
				if(!$subject_sub[$v['id']]){
					$check_id = $v['id'];
					break;
				}
			}
			$sub_num = count($subjects) -count($subject_sub);
			$re = array('status'=>0,'msg'=>'您还有'.$sub_num.'道题没有回答','check_id'=>$check_id);
		}
		else{
			foreach($subjects as $k=>$v){
				foreach($subject_sub as $ke=>$va){
					if($v['id'] == $ke){
						if($v['num']==1){//单选判断正确
							foreach($v['values'] as $key => $val){
								if($val['selected'] == 1){
									if($key == $va[0]){
										$num ++;
									}
								}
							}
						}else if($v['num']>1){//多选判断正确
							foreach ($v['values'] as $key => $val) {
								if($val['selected'] == 1){
									$select[]=$key;
								}
							}
							foreach ($subject_sub[$v['id']] as $key => $value) {
								$selected[] = $value;
							}
							if($select == $selected){
								$num ++;
							}
						}else{//没正确答案的
							$num ++;
						}
					}
				}
			}

			//写入反馈表
			$fan_data = array(
					'wid'=>$wid,
					'answer'=>$Json->encode_unescaped_unicode($subject_sub),
					'user'=>$_SESSION['phone'],
					'right_num'=>$num,
					'create_time'=>date('Y-m-d',time()),
				);
			$Db->insert($Base->table('wenjuan_fankui'),$fan_data);

			$re = array('status'=>1,'msg'=>'您一共答对了'.$num.'道题！','href'=>'/wenjuan?wid='.$wid.'&act=jiang&step=choujiang');
		}
		echo $Json->encode($re);
		exit;
	}
	else{
		$tp->assign('subjects',$subjects);
	}
}
$tp->assign('phone',$_SESSION['phone']);
$tp->assign('step',$step);
$tp->assign('wid',$wid);
$tp->assign('act',$act);
$tp->assign('wenjuan',$wenjuan);
$tp->display($_lang['moban'].'/index/wenjuan.lbi');
exit;	
?>